// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef NETDATA_STREAM_HANDSHAKE_H
#define NETDATA_STREAM_HANDSHAKE_H

#define HTTP_HEADER_SIZE 8192

#define STREAMING_PROTOCOL_VERSION "1.1"
#define START_STREAMING_PROMPT_V1 "Hit me baby, push them over..."
#define START_STREAMING_PROMPT_V2 "Hit me baby, push them over and bring the host labels..."
#define START_STREAMING_PROMPT_VN "Hit me baby, push them over with the version="

#define START_STREAMING_ERROR_SAME_LOCALHOST "Don't hit me baby, you are trying to stream my localhost back"
#define START_STREAMING_ERROR_ALREADY_STREAMING "This GUID is already streaming to this server"
#define START_STREAMING_ERROR_NOT_PERMITTED "You are not permitted to access this. Check the logs for more info."
#define START_STREAMING_ERROR_BUSY_TRY_LATER "The server is too busy now to accept this request. Try later."
#define START_STREAMING_ERROR_INTERNAL_ERROR "The server encountered an internal error. Try later."
#define START_STREAMING_ERROR_INITIALIZATION "The server is initializing. Try later."

#define STREAM_STATUS_CONNECTED                     "CONNECTED"
#define STREAM_STATUS_ALREADY_CONNECTED             "ALREADY CONNECTED"
#define STREAM_STATUS_DISCONNECTED                  "DISCONNECTED"
#define STREAM_STATUS_RATE_LIMIT                    "RATE LIMIT TRY LATER"
#define STREAM_STATUS_INITIALIZATION_IN_PROGRESS    "INITIALIZATION IN PROGRESS RETRY LATER"
#define STREAM_STATUS_INTERNAL_SERVER_ERROR         "INTERNAL SERVER ERROR DROPPING CONNECTION"
#define STREAM_STATUS_DUPLICATE_RECEIVER            "DUPLICATE RECEIVER DROPPING CONNECTION"
#define STREAM_STATUS_CANT_REPLY                    "CANT REPLY DROPPING CONNECTION"
#define STREAM_STATUS_NO_HOSTNAME                   "NO HOSTNAME PERMISSION DENIED"
#define STREAM_STATUS_NO_API_KEY                    "NO API KEY PERMISSION DENIED"
#define STREAM_STATUS_INVALID_API_KEY               "INVALID API KEY PERMISSION DENIED"
#define STREAM_STATUS_NO_MACHINE_GUID               "NO MACHINE GUID PERMISSION DENIED"
#define STREAM_STATUS_MACHINE_GUID_DISABLED         "MACHINE GUID DISABLED PERMISSION DENIED"
#define STREAM_STATUS_INVALID_MACHINE_GUID          "INVALID MACHINE GUID PERMISSION DENIED"
#define STREAM_STATUS_API_KEY_DISABLED              "API KEY DISABLED PERMISSION DENIED"
#define STREAM_STATUS_NOT_ALLOWED_IP                "NOT ALLOWED IP PERMISSION DENIED"
#define STREAM_STATUS_LOCALHOST                     "LOCALHOST PERMISSION DENIED"
#define STREAM_STATUS_PERMISSION_DENIED             "PERMISSION DENIED"
#define STREAM_STATUS_BAD_HANDSHAKE                 "BAD HANDSHAKE"
#define STREAM_STATUS_TIMEOUT                       "TIMEOUT"
#define STREAM_STATUS_CANT_UPGRADE_CONNECTION       "CANT UPGRADE CONNECTION"

typedef enum {
    STREAM_HANDSHAKE_OK_V3 = 3, // v3+
    STREAM_HANDSHAKE_OK_V2 = 2, // v2
    STREAM_HANDSHAKE_OK_V1 = 1, // v1
    STREAM_HANDSHAKE_NEVER = 0, // never tried to connect
    STREAM_HANDSHAKE_ERROR_BAD_HANDSHAKE = -1,
    STREAM_HANDSHAKE_ERROR_LOCALHOST = -2,
    STREAM_HANDSHAKE_ERROR_ALREADY_CONNECTED = -3,
    STREAM_HANDSHAKE_ERROR_DENIED = -4,
    STREAM_HANDSHAKE_ERROR_SEND_TIMEOUT = -5,
    STREAM_HANDSHAKE_ERROR_RECEIVE_TIMEOUT = -6,
    STREAM_HANDSHAKE_ERROR_INVALID_CERTIFICATE = -7,
    STREAM_HANDSHAKE_ERROR_SSL_ERROR = -8,
    STREAM_HANDSHAKE_ERROR_CANT_CONNECT = -9,
    STREAM_HANDSHAKE_BUSY_TRY_LATER = -10,
    STREAM_HANDSHAKE_INTERNAL_ERROR = -11,
    STREAM_HANDSHAKE_INITIALIZATION = -12,
    STREAM_HANDSHAKE_DISCONNECT_HOST_CLEANUP = -13,
    STREAM_HANDSHAKE_DISCONNECT_STALE_RECEIVER = -14,
    STREAM_HANDSHAKE_DISCONNECT_SHUTDOWN = -15,
    STREAM_HANDSHAKE_DISCONNECT_NETDATA_EXIT = -16,
    STREAM_HANDSHAKE_DISCONNECT_PARSER_EXIT = -17,

    STREAM_HANDSHAKE_DISCONNECT_UNKNOWN_SOCKET_READ_ERROR = -18,
    STREAM_HANDSHAKE_DISCONNECT_PARSER_FAILED = -19,
    STREAM_HANDSHAKE_DISCONNECT_RECEIVER_LEFT = -20,
    STREAM_HANDSHAKE_DISCONNECT_ORPHAN_HOST = -21,
    STREAM_HANDSHAKE_NON_STREAMABLE_HOST = -22,

    STREAM_HANDSHAKE_DISCONNECT_NOT_SUFFICIENT_READ_BUFFER = -23,
    STREAM_HANDSHAKE_DISCONNECT_NOT_SUFFICIENT_SEND_BUFFER = -25,

    STREAM_HANDSHAKE_DISCONNECT_NOT_SUFFICIENT_SENDER_COMPRESSION_FAILED = -24,
    STREAM_HANDSHAKE_DISCONNECT_SOCKET_EOF = -26,
    STREAM_HANDSHAKE_DISCONNECT_SOCKET_READ_FAILED = -27,
    STREAM_HANDSHAKE_DISCONNECT_SOCKET_TIMEOUT = -28,
    STREAM_HANDSHAKE_DISCONNECT_SOCKET_ERROR = -29,
    STREAM_HANDSHAKE_DISCONNECT_SOCKET_WRITE_FAILED = -30,
    STREAM_HANDSHAKE_DISCONNECT_SOCKET_CLOSED_BY_REMOTE_END = -31,
    STREAM_HANDSHAKE_ERROR_HTTP_UPGRADE = -32,
    STREAM_HANDSHAKE_NO_HOST_IN_DESTINATION = -33,
    STREAM_HANDSHAKE_CONNECT_TIMEOUT = -34,
    STREAM_HANDSHAKE_CONNECTION_REFUSED = -35,
    STREAM_HANDSHAKE_CANT_RESOLVE_HOSTNAME = -36,
    STREAM_HANDSHAKE_PREPARING = -37,
    STREAM_HANDSHAKE_CONNECTING = -38,
    STREAM_HANDSHAKE_CONNECTED = -39,
    STREAM_HANDSHAKE_EXITING = -40,
    STREAM_HANDSHAKE_NO_STREAM_INFO = -41,

} STREAM_HANDSHAKE;

const char *stream_handshake_error_to_string(STREAM_HANDSHAKE reason);

#endif //NETDATA_STREAM_HANDSHAKE_H
